package com.supreme.simpleserver.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.supreme.commonmybatis.entity.primary.MBGoods;
import com.supreme.commonmybatis.mapper.GoodsMapper;
import com.supreme.simpleserver.service.mybatis.MBGoodsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class GoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private MBGoodsService mbGoodsService;

    public MBGoods findOneSvc(Long goodsId) {
        return mbGoodsService.getById(goodsId);
    }

    @Transactional
    public MBGoods findOneSleepSvc(Long goodsId) {
        MBGoods goods = this.mbGoodsService.getById(goodsId);
        try {
            Thread.sleep(10 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return goods;
    }

    @Transactional
    public boolean updateGoodsSvc(Long goodsId, Integer updateStock) {
        MBGoods goods = this.mbGoodsService.getById(goodsId);
        return true;
    }

    @Transactional
    public boolean deductGoodsStockSvc(Long goodsId, Integer deductStock, Long sleepSecond) {
        MBGoods goods = this.mbGoodsService.getById(goodsId);
        return true;
    }

    @Transactional
    public int deductGoodsStockAmount(Long goodsId, Integer deductStock) {
        MBGoods goods = this.mbGoodsService.getById(goodsId);
        return this.goodsMapper.deductGoodsStockAmount(goodsId, 1, deductStock);
    }

    public MBGoods selectGoods(String goodsName) {
        QueryWrapper<MBGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_name", goodsName);
        return this.goodsMapper.selectOne(queryWrapper);
    }
}
